package top.jolyoulu.corecommon.queue.delayed;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import top.jolyoulu.corecommon.queue.delayed.observer.DelayedTaskSubject;

/**
 * @Author: JolyouLu
 * @Date: 2023/4/15 18:51
 * @Description 延迟队列消费者
 */
@Slf4j
@Getter
public class DelayedTaskConsumer implements Runnable {

    /**
     * 观察者主持信息
     */
    private final DelayedTaskSubject delayedTaskSubject;

    private final DelayedTaskQueue delayedTaskQueue;

    public DelayedTaskConsumer(DelayedTaskSubject delayedTaskSubject, DelayedTaskQueue delayedTaskQueue) {
        this.delayedTaskSubject = delayedTaskSubject;
        this.delayedTaskQueue = delayedTaskQueue;
    }

    @Override
    public void run() {
        Thread currentThread = Thread.currentThread();
        log.info("{} ==> started",currentThread.getName());
        while (true){
            try {
                //获取一个任务
                DelayedTaskEntity<?> take = delayedTaskQueue.take();
                delayedTaskSubject.notifyObserver(take);
            }catch (Exception e){
                log.error("",e);
            }
        }
    }
}
